package com.tarena.lbs.user.dao.repository.impl;

import com.alibaba.nacos.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.user.dao.mapper.AdminMapper;
import com.tarena.lbs.user.dao.repository.AdminRepository;
import com.tarena.lbs.user.pojo.po.AdminPO;
import com.tarena.lbs.user.pojo.query.AdminQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class AdminRepositoryImpl implements AdminRepository {
    @Autowired
    private AdminMapper adminMapper;

    @Override
    public AdminPO selectById(Integer id) {
        System.out.println(id);
        return adminMapper.selectById(id);
    }

    @Override
    public PageInfo<AdminPO> pageList(AdminQuery query) {
        QueryWrapper<AdminPO> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(query.getNickname())){
            queryWrapper.like("nickname", query.getNickname());
        }
        if (StringUtils.isNotBlank(query.getAccountPhone())){
            queryWrapper.like("account_phone", query.getAccountPhone());
        }
        PageHelper.startPage(query.getPageNo(), query.getPageSize());
        List<AdminPO> list = adminMapper.selectList(queryWrapper);
        return new PageInfo<>(list);
    }

    @Override
    public Long selectByAccountPhone(String accountPhone) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("account_phone", accountPhone);
        return adminMapper.selectCount(queryWrapper);
    }

    @Override
    public void save(AdminPO adminPO) {
        adminMapper.insert(adminPO);
    }


    @Override
    public List<AdminPO> getPasswordByPhone(String phone) {
        QueryWrapper<AdminPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account_phone",phone);
        List<AdminPO> list  =  adminMapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public void delete(Integer id) {
        adminMapper.deleteById(id);
    }

    //查询数据用于回显
    @Override
    public void resetPassword(AdminPO adminPO) {
        adminMapper.updateById(adminPO);
    }


}
